import OpenEXR
import Imath
import math
import numpy as np


def readExr(exrfile): 
    
    
    
    exrfile = str(exrfile)
    
    file = OpenEXR.InputFile(exrfile)
    
    pt = Imath.PixelType(Imath.PixelType.FLOAT)
    
    dw = file.header()['dataWindow']
    
    size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
    
    R,G,B = [np.fromstring( file.channel(c, pt), dtype = np.float32) for c in ("R","G","B")]
    
    R = R.reshape(size[1],size[0]).T
    
    G = G.reshape(size[1],size[0]).T
    
    B = B.reshape(size[1],size[0]).T
    
    L = 0.2125*R + 0.7154*G + 0.0721*B
    
    return R,G,B,L,size